package cn.stylefeng.guns.modular.course.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.course.entity.CourseVideo;
import cn.stylefeng.guns.modular.course.mapper.CourseVideoMapper;
import  cn.stylefeng.guns.modular.course.service.CourseVideoService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author typ
 * @since 2021-04-24
 */
@Service
public class CourseVideoServiceImpl extends ServiceImpl<CourseVideoMapper, CourseVideo> implements CourseVideoService {

    @Override
    public void add(CourseVideo param){
        CourseVideo entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CourseVideo param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CourseVideo param){
        CourseVideo oldEntity = getOldEntity(param);
        CourseVideo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CourseVideo findBySpec(CourseVideo param){
        return null;
    }

    @Override
    public List<CourseVideo> findListBySpec(CourseVideo param){
        return baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(CourseVideo param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(CourseVideo param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private CourseVideo getOldEntity(CourseVideo param) {
        return this.getById(getKey(param));
    }

    private CourseVideo getEntity(CourseVideo param) {
        CourseVideo entity = new CourseVideo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
